
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Michael Terada
 */
public class CityState {
        double latitude = 0;
        double longitude = 0;
        
        public CityState(String address, String city, String state) {
        String baseurl = "http://maps.googleapis.com/maps/api/geocode/json?address=";
        String currenturl;
        String parsedAddress = address.replace(' ', '+');
        String parsedCity = city.replace(' ', '+');
        String parsedState = state.replace(' ', '+');
        latitude = 0;
        longitude = 0;
        try {
            currenturl = baseurl + parsedAddress + "," + parsedCity + ","+ parsedState + "&sensor=false";
            URL url = new URL(currenturl);
            BufferedReader buffreader = new BufferedReader(new InputStreamReader(
                    ((HttpURLConnection) url.openConnection()).getInputStream(), "UTF8"));

            JsonObject jsonObject = new JsonParser().parse(buffreader).getAsJsonObject();
            latitude = jsonObject.get("results").getAsJsonArray().get(0).getAsJsonObject().get("geometry").getAsJsonObject().get("location").getAsJsonObject().get("lat").getAsDouble();
            longitude = jsonObject.get("results").getAsJsonArray().get(0).getAsJsonObject().get("geometry").getAsJsonObject().get("location").getAsJsonObject().get("lng").getAsDouble();          
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        
    public double getLat() {
        return latitude;
    }
    public double getLong() {
        return longitude;
    }
}
